#include "cpu/exec/helper.h"
#include "monitor/monitor.h"

make_helper(inv) {
	/* invalid opcode */

	uint32_t temp[8];
	temp[0] = instr_fetch(eip, 4);
	temp[1] = instr_fetch(eip + 4, 4);

	uint8_t *p = (void *)temp;
	printf("invalid opcode(eip = 0x%08x): %02x %02x %02x %02x %02x %02x %02x %02x ...\n\n",
			eip, p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);

	extern char logo [];
	/*printf("There are two cases which will trigger this unexpected exception:\n\
1. The instruction at eip = 0x%08x is not implemented.\n\
2. Something is implemented incorrectly.\n", eip);*/
	printf("Find this eip value(0x%08x) in the disassembling result to distinguish which case it is.\n\n", eip);
	printf("If it is the first case, \33[1;31mgo to SLEEP\n%s\nfor more details.\n\n\33[1;32mIf it is the second case, remember:\n\
* You are not sleeping!\n\
* You should go to sleep!\33[0m\n\n", logo);

	assert(0);
}

make_helper(hlt) {
    for (;!cpu.INTR;);
    return 1;
}

make_helper(nemu_trap) {
	print_asm("nemu trap (eax = %d)", cpu.eax);
    int i;
	switch(cpu.eax) {
		case 2:
            for (i = 0; i < cpu.edx; i ++ )
                putchar(swaddr_read(cpu.ecx + i, 1, 3));
		   	break;

		default:
			printf("\33[1;31mnemu: HIT %s TRAP\33[0m at eip = 0x%08x\n\n",
					(cpu.eax == 0 ? "GOOD" : "BAD"), cpu.eip);
			nemu_state = END;
	}

	return 1;
}

